//
//  HomeActivityCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import MYModule
import RxCocoa
import RxSwift


open class HomeActivityCell: HomeBaseCell {
    
    
    let stepsDataView = RecordView()
    
    let kCalDataView = RecordView()
    
    let timeDataView = RecordView()
     
    let progressView = ArcProgressBarView()
    
    public override func makeUI() {
        super.makeUI()
        
        let iconName: String = "ic_home_today_activity"
        let title = "activity_title".language
        let stepsTitle = "unit_step".language
        let stepsIconName = "ic_home_today_steps_steps"
        let kCalTitle = "activity_calories".language
        let kcalIconName = "ic_home_today_steps_calorie"
        let timeTitle = "activity_duration".language
        let timeIconName = "ic_home_today_steps_duration"
        
        iconImagView.image = UIImage(named: iconName)
        titleLabel.text = title
        stepsDataView.titleLabel.text = stepsTitle
        kCalDataView.titleLabel.text = kCalTitle
        timeDataView.titleLabel.text = timeTitle
        
        stepsDataView.icon.image = UIImage(named: stepsIconName)
        kCalDataView.icon.image = UIImage(named: kcalIconName)
        timeDataView.icon.image = UIImage(named: timeIconName)
        
        bgView.addSubview(progressView)
        bgView.addSubview(stepsDataView)
        bgView.addSubview(kCalDataView)
        bgView.addSubview(timeDataView)
        

        progressView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(dateLabel.snp.bottom).offset(17)
            make.width.equalTo(120)
            make.height.equalTo(100)
        }
        
        let mid = (UIScreen.main.bounds.width - 32.0)/6.0
        stepsDataView.snp.makeConstraints { make in
            make.centerX.equalTo(mid)
            make.top.equalTo(progressView.snp.bottom).offset(16)
            make.bottom.equalTo(bgView.snp.bottom).offset(-20).priority(888)
            
        }
        
        kCalDataView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(stepsDataView.snp.top)
            make.bottom.equalTo(stepsDataView.snp.bottom)
        }
        
        timeDataView.snp.makeConstraints { make in
            make.centerX.equalTo(mid * 5.0)
            make.top.equalTo(stepsDataView.snp.top)
            make.bottom.equalTo(stepsDataView.snp.bottom)
        }
        
        
    }
    
    public override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let activityViewModel = viewModel as? HomeActivityViewModel else { return }
        
        let disposeBag = DisposeBag()
        
        activityViewModel.UIReplay.subscribe(onNext: {[weak self] in
            
            self?.dateLabel.text = $0.date
            
            self?.stepsDataView.targetUnitLabel.text = $0.step?.intTargetTitle
            self?.kCalDataView.targetUnitLabel.text = $0.calories?.intTargetTitle
            self?.timeDataView.targetUnitLabel.text = $0.duration?.intTargetTitle
            
            self?.stepsDataView.unitLabel.text = $0.step?.intTitle
            self?.kCalDataView.unitLabel.text = $0.calories?.intTitle
            self?.timeDataView.unitLabel.text = $0.duration?.intTitle
            
            self?.progressView.stepsView.progress = $0.step?.progress
            self?.progressView.caloriesView.progress = $0.calories?.progress
            self?.progressView.durationView.progress = $0.duration?.progress

            
        }).disposed(by: disposeBag)
        
       
    }
    
    public class ProgressView: View {
        
        public class Config {
            // 进度（0 - 1） 比如 50%,就传 0.5， 100%就是 1
            var progress: Double = Double.pi
            var color: UIColor?
            let backColor = UIColor.assist_A9A9A9
            var radius: CGFloat = 1.0
            var holeRadius: CGFloat = 0.0
            
            var lineWidth: CGFloat {
                return holeRadius/radius
            }
        }
        
        let lineWidth: CGFloat = 8.0
        
        var stepConfig = Config()
        var kCalConfig = Config()
        var timeConfig = Config()
        
        private var stepPieChartView = DFPieChartView(frame: CGRect.zero)
        private var kCalPieChartView = DFPieChartView(frame: CGRect.zero)
        private var timePieChartView = DFPieChartView(frame: CGRect.zero)
        
      
        fileprivate var configs: [Config] {
            
          return [stepConfig, kCalConfig, timeConfig]
        }
        
        fileprivate var pieChartViews: [DFPieChartView] {
            return [stepPieChartView, kCalPieChartView, timePieChartView]
        }
        
        
        public override func makeUI() {
            super.makeUI()
            
            
            let colors: [UIColor?]  = [UIColor.Step.theme, UIColor.subTheme, UIColor.theme]
            let radiuses = [50.0, 40.0, 30.0]
            let holeRadius = [40.0, 30.0, 20.0]
            
            
            
            
            for i in 0..<configs.count {
                
                let config = configs[i]
                config.color = colors[i]
                config.radius = radiuses[i]
                config.holeRadius = holeRadius[i]

                let pieChartView = pieChartViews[i]
                
                pieChartView.holeRadiusPercent = config.lineWidth
//
                addSubview(pieChartView)
                pieChartView.snp.makeConstraints { make in
                    
                    make.centerX.equalToSuperview()
                    make.centerY.equalToSuperview()
                    make.width.equalTo(2 * config.radius)
                    make.height.equalTo(2 * config.radius)
                    
                }
              
               

                
            }
            
            
        }
        
        
        /// 更新 progress 之后必须调用才能显示最新的刻度
        func reloadData() {
            
            
            
            for i in 0..<configs.count {
                
                let config = configs[i]
                let color = config.color!
                let backColor = config.backColor
                let progress = config.progress
                let backProgress = 1.0 - progress
                
                let pieChartView = pieChartViews[i]
                
                
                pieChartView.setData([progress, backProgress], colors: [color, backColor], sliceSpace: 0)
                
            }
        }
        
    }
    
    
    public class RecordView: View {
        
        let titleLabel = Label()
        
        let icon = ImageView()
        
        let targetUnitLabel = Label()
        
        let unitLabel = Label()
        
        public override func makeUI() {
            super.makeUI()
            
            titleLabel.font = Fonts.systemFont(ofSize: 12)
            titleLabel.textColor = UIColor.assist_D0D0D0
            
            targetUnitLabel.font = Fonts.systemFont(ofSize: 10)
            targetUnitLabel.textColor = UIColor.assist_A9A9A9
            
            unitLabel.font = Fonts.boldSystemFont(ofSize: 16)
            unitLabel.textColor = UIColor.assist_222222
            
            addSubview(titleLabel)
            addSubview(icon)
            addSubview(unitLabel)
            addSubview(targetUnitLabel)
            
            titleLabel.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.centerX.equalToSuperview()
                make.width.lessThanOrEqualToSuperview().priority(999)
            }
            
           
            icon.snp.makeConstraints { make in
                make.right.equalTo(unitLabel.snp.left).offset(-7)
                make.width.equalTo(14)
                make.height.equalTo(16)
                make.top.equalTo(titleLabel.snp.bottom).offset(5)
                make.left.greaterThanOrEqualToSuperview().priority(999)
            }
            
            unitLabel.snp.makeConstraints { make in
                make.centerY.equalTo(icon.snp.centerY)
                make.centerX.equalTo(titleLabel.snp.centerX).offset(11.5)
                make.width.lessThanOrEqualToSuperview().priority(999)
            }
            
            targetUnitLabel.snp.makeConstraints { make in
                make.top.equalTo(icon.snp.bottom).offset(4)
                make.centerX.equalTo(titleLabel.snp.centerX)
                make.bottom.equalToSuperview()
                make.right.lessThanOrEqualToSuperview().priority(999)
            }
            
            
            
        }
        
    }
    
}
